package com.gosophia.govDoc.action.transientDoc;

import org.springframework.context.annotation.Scope;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.gosophia.commons.PropertyFilter;
import com.gosophia.commons.PropertyFilter.OperatorType;
import com.gosophia.commons.web.action.BaseAjaxAction;
import com.gosophia.govDoc.dto.TransientDocumentDTO;
import com.gosophia.govDoc.ws.TransientDocumentWebService;
import com.gosophia.springsecurity.UserDetailsImpl;

@Scope("prototype")
@Controller("GetPageTranDocAction")
public class GetPageTranDocAction extends BaseAjaxAction<TransientDocumentDTO>{

	 /**
	 * 
	 */
	private static final long serialVersionUID = 5246586054874920422L;

	private TransientDocumentWebService transientDocumentWebService;
	
	private Boolean searchFlg = false;
		
	 private Integer pageNo;

	 private Integer pageSize;
	    
	@Override
	public TransientDocumentDTO getDetailData() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String _execute() throws Exception {
		   UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder
           .getContext().getAuthentication().getPrincipal();
		   if(pageNo==null) {
		    	pageNo = 1;
		    }
		    if(searchFlg.booleanValue() == false) {
		    	this.page = transientDocumentWebService.findSampleTransientDocumentsByPage(pageNo, pageSize, this.searchString, userDetails.getUsername(), orderConditions);
		    }else {
		    	if(propertyFilters.size()!=0&&propertyFilters!=null) {
			       for(PropertyFilter p1:propertyFilters) {
			    	 if("creatDocTime".equals(p1.getPropertyName())){
			    		       p1.setToCharFormat("yyyy-MM-dd hh24:mi:ss");
			    	  }
			    	 if("editDocTime".equals(p1.getPropertyName())){
		    		       p1.setToCharFormat("yyyy-MM-dd hh24:mi:ss");
		    	  }
			       }
			       PropertyFilter p2 = new PropertyFilter(
		 					"creator.userName", OperatorType.EQ, userDetails.getUsername());
			       propertyFilters.add(p2);	   
			    	}
		    	this.page = transientDocumentWebService.findTransientDocumentsByPage(pageNo, pageSize, propertyFilters, orderConditions);
		    }
		  
		    if(this.page.getTotalCount()<=0) {
		    	super.getRequestResult().setRsltCode("1");
		    }else {
		    	super.getRequestResult().setRsltCode("0");
		    }
		return SUCCESS;
	}

}
